package com.woniuxy.usercoachassemble.configuration;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitmqConfiger {

    /**
     * 死信交换机的名字
     */
   public   static  final String ORDERS_SCAN_EXCHANGE="yoga_orders_scan_exchange";
    /**
     * 处理订单业务交换机的名字
     */
    public  static  final String ORDERS_SERVICE_EXCHANGE="yoga_orders_service_exchange";
    /**
     * 死信订单队列的名字
     */
    public static final String  ORDERS_SCAN_QUEUE="yoga_orders_scan_queue";
    /**
     * 处理订单业务队列名字
     */
    public static final String  ORDERS_SERVICE_QUEUE="yoga_orders_service_queue";

    /**
     * 死信路由名字
     */
    public static final String ORDERS_SCAN_KEY="yoga_orders_scan_key";
    /**
     * 业务路由名字
     */
    public static final String ORDERS_SERVICE_KEY="yoga_orders_service_key";
    /**
     * 创建死信交换机
     * @return
     */
    @Bean
    public DirectExchange delExchange() {
        return new DirectExchange(ORDERS_SCAN_EXCHANGE);
    }
    /**
     * 创建订单业务交换机
     */
    @Bean
    public DirectExchange orderExchange() {
        return new DirectExchange(ORDERS_SERVICE_EXCHANGE);
    }
    /**
     * 创建死信队列
     * @return
     */
    @Bean
    public Queue delQueue() {

        return new Queue(ORDERS_SCAN_QUEUE);
    }

    /**
     * 创建订单业务队列 核心操作一
     */
    @Bean
    public Queue orderQueue() {
        Map<String, Object> arguments = new HashMap<>(2);
        // 绑定死信交换机
        arguments.put("x-dead-letter-exchange", ORDERS_SCAN_EXCHANGE);
        // 绑定死信路由key
        arguments.put("x-dead-letter-routing-key", ORDERS_SCAN_KEY);


        return new Queue(ORDERS_SERVICE_QUEUE, true, false, false, arguments);
    }

    /**
     * 绑定业务队列到业务交换机
     * @return
     */
    @Bean
    public Binding orderBinding() {
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(ORDERS_SERVICE_KEY);
    }
    /**
     * 绑定死信队列到死信交换机
     */
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(delQueue()).to(delExchange()).with(ORDERS_SCAN_KEY);
    }



}
